<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!--登陆状态校验 -->
	<script src="js/checkLogin.js"></script>
</head>
<body class="container">
    <div class="row">
        <ol class="breadcrumb">
            <li><a href="javascript:void(0)">用户管理</a></li>
            <li class="active">管理员列表</li>
        </ol>
    </div>
    <div class="row">
        <form class="form-inline">
            <div class="form-group">
                <label for="admin_name">名称</label>
                <input type="text" class="form-control" name="admin_name" id="admin_name" placeholder="管理员名称">
            </div>
            <div class="form-group">
                <label for="adminStatus">状态</label>
                <select id="adminStatus" name="adminStatus" class="form-control">
                    <option value="no">全部</option>
                    <option value="1">正常</option>
                    <option value="2">废弃</option>
                </select>
            </div>
            <button id="searchBtn" type="button" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
            <button id="addBtn" type="button" class="btn btn-default btn-success">添加管理员<span class="glyphicon glyphicon-plus"></span></button>
        </form>
    </div>
    <div class="row">
        <table id="adminList" class="table">

        </table>

        <div id="opBar">
            <a onclick="inputEdit(this)" href="javascript:void(0)">编辑</a> 
        </div>
    </div>
    <div class="row">
        <nav id="pageBar" aria-label="Page navigation">
            <ul class="pagination">
            </ul>
        </nav>
    </div>

<!-- 编辑的弹窗 -->
    <!-- Modal -->
    <div class="modal fade" id="adminEditWin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 h4 class="modal-title" id="myModalLabel">添加管理员</h4>
            </div>
            <div class="modal-body">
                    <form>
                        <!-- 隐藏域，设置管理员编号，编辑的时候使用 -->
                        <input type="hidden" name="adminId" id="adminId">
                        <div class="form-group">
                            <label for="adminName">管理员名称</label>
                            <input type="text" class="form-control" name="adminName" placeholder="管理员名称">
                        </div>
                        <div class="form-group">
                            <label for="adminPass">登陆密码</label>
                            <input type="password" class="form-control" name="adminPass" placeholder="登陆密码">
                        </div>
                        <div class="form-group">
                            <label for="exampleInputFile">状态</label>
                            <select id="adminStatus" name="adminStatus" class="form-control">
                                <option value="1">正常</option>
                                <option value="2">废弃</option>
                            </select>
                        </div>
                    </form>
            </div>
            <div class="modal-footer">
                <button type="button" id="subBtn" class="btn btn-primary">提交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
            </div>
        </div>
    </div>
<script>
    var adminListUrl = "http://192.168.4.2:8088/paper/admin/query";
    var columns = [
        {field:"adminId",title:"#"},
        {field:"adminName",title:"登录名"},
        {field:"adminCreated",title:"创建时间",formatter:function(value){
            return value?value.substring(0,value.length-2):"";
        }},
        {field:"adminStatus",title:"状态",formatter:function(value){
            return (value+"")=="1"?'正常':'废弃';
        }},
        {field:'st9527-options',title:"操作",barId:'opBar'}
    ];
    $(document).ready(function(){
        queryData();
        //给搜索按钮添加事件
        $("#searchBtn").bind("click",function(){
            queryData()
        });
    });

    function queryData(){
        var adminName = $("#adminName").val();
        var adminStatus = $("#adminStatus").val();
        st9527.table("adminList",columns,adminListUrl,{page:1,size:10,adminName:adminName,adminStatus:adminStatus},"pageBar");
    }

    $("#addBtn").bind("click",function(){
        // 清空表单
        $("#adminEditWin form")[0].reset();
        // 设置标题
        $("#adminEditWin .modal-title").text("添加管理员");
        //打开窗体
        $("#adminEditWin").modal('show');
    });
    
    // 提交按钮的事件
    $("#subBtn").bind("click",function(){
        var url = "";
        // 获取隐藏域中的值
        var adminId = $("#adminId").val();
        console.log(adminId);
        if(adminId){
            url = "http://192.168.4.2:8088/paper/admin/edit";
        }else{
            url = "http://192.168.4.2:8088/paper/admin/save";
        }
        // 提交表单
        $.post(url,$("#adminEditWin form").serialize(),function(res){
            if(res.code==200){
                msgAlert(res.msg,"提示");
                //关闭窗体
                $("#adminEditWin").modal('toggle');
            }else{
                msgAlert(res.msg,"提示");
            }
        });
    });
    // 编辑按钮的事件
    function inputEdit(ele){
        var queryAdminUrl = "http://192.168.4.2:8088/paper/admin/queryById";
        var adminId = $(ele).parent().attr("dataid");
        if(adminId){
            // 加载admin对象
            $.get(queryAdminUrl,{adminId:adminId},function(res){
                if(res.code==200 && res.obj){
                    // 填充表单
                    $("#adminEditWin [name=adminId]").val(res.obj.adminId);
                    $("#adminEditWin [name=adminName]").val(res.obj.adminName);
                    $("#adminEditWin [name=adminStatus]").val(res.obj.adminStatus);
                    // tips : 密码不能填充。
                    $("#adminEditWin [name=adminPass]").attr("placeholder","不修改，则留空");
                    //填充完成后打开窗体
                    $("#adminEditWin").modal('show');
                }
            });
        }
    }
</script>
</body>
</html>